#include <vector>
#include <iostream>
#include <thread>

// 向量扩充
typedef struct{
    int idx;
    int dim;
    int row_nums;
    int size; // 需要复制的数据的个数
    float *a; // 原数据,形状必须是向量
    float *b;
}Cot;

void copy_t(Cot *p){
    if (p->dim == 0){
        for (int i = 0; i < p->size; i++) {
            p->b[p->idx * p->size + i] = p->a[i];
        }
    }else if(p->dim == 1){
        for (int i = 0; i < p->size; i++) {
            p->b[i * p->row_nums + p->idx] = p->a[i];
        }
    }
    
}

void sscp(float *a, float *b, int a_size, int dim, int nums){
    int thread_num = std::thread::hardware_concurrency();
    std::vector<std::thread> threads(thread_num);
    std::vector<Cot> mes(thread_num);

    int blocksdim;
    if (nums%thread_num != 0) {
        if (nums < thread_num) {
            for (int i = 0; i < nums; i++) {
                int end = i + 1;
                int start = i;
                mes[i] = Cot{i, dim, nums, a_size, a, b};
            }
        }else{
            blocksdim = nums/(thread_num -1);
            for (int i = 0; i < thread_num; i++) {
                int end;
                if (i == thread_num - 1) {
                    end = nums;
                }
                else{
                    end = blocksdim * (i + 1);
                }
                int start = blocksdim * i;
                
                mes[i] = Cot{i, dim, nums, a_size, a, b};
            }
        }
    }else{
        blocksdim = nums/thread_num;

        for (int i = 0; i < thread_num; i++) {
                int end = blocksdim * (i + 1);
                int start = blocksdim * i;
                mes[i] = Cot{i, dim, nums, a_size, a, b};
            }
    }
    if (nums < thread_num) {
        thread_num = nums;
    }
    for (int i = 0; i < thread_num; i++) {
        threads[i] = std::thread(copy_t, &mes[i]);
    }
    for (int i = 0; i < thread_num; i++) {
        threads[i].join();
    }
}

int main(){
    float a[3] = {1, 2, 3};
    float b[3*4] = {0};

    sscp(a, b, 3, 1, 4);

    for (int i = 0; i < 3; i++) {
        for(int j = 0; j < 4; j++){
            std::cout << b[i * 4 + j] << " ";
        }
        std::cout << std::endl;
    }
}
